#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=2e5+10,M=2100;
int T,n,m,a[N],dp[M][M],c[N];
inline bool cmp(const int p,const int q){
	return (a[p]==a[q])?(p<q):(a[p]>a[q]);
}
inline void solve1(){
	int ans=0;
	for(int s=(1<<n),pre1,pre2,sum;s;--s){
		pre1=pre2=sum=0;
		for(int i=1;i<=n;++i)
			if(s&(1<<(i-1))){
				if(pre1==a[i])sum+=a[i];
				else pre1=a[i];
			}else{
				if(pre2==a[i])sum+=a[i];
				else pre2=a[i];
			}
		ans=max(ans,sum);
	}
	printf("%lld\n",ans);
}
inline void solve3(){
	for(int i=1;i<=n;++i)c[i]=i;
	sort(c+1,c+n+1);
	for(int i=1;i<=n;++i)
		if(a[c[i]]==a[c[i-1]]){
			printf("%lld\n",a[c[i]]);
			return;
		}
}
inline void solve2(){
	int ans=0;
	for(int i=1;i<=n;++i){
		for(int j=1;j<i-1;++j)
			dp[i][j]=max(dp[i][j],dp[i-1][j]+ ((a[i]==a[i-1])?(a[i]):(0)) );
		for(int j=1;j<i-1;++j)
			dp[i][i-1]=max(dp[i][i-1],dp[i-1][j]+(a[i]==a[j]?a[i]:0));
		for(int j=1;j<i;++j)ans=max(ans,dp[i][j]);
	}
	printf("%lld\n",ans);
}
signed main(){
	freopen("color.in","r",stdin);
	freopen("color.out","w",stdout);
	scanf("%lld",&T);
	while(T--){
		scanf("%lld",&n);
		for(int i=1;i<=n;++i)scanf("%lld",&a[i]);
		if(n<=20)solve1();
		else if(n<=2000)solve2();
		else solve3();
	}
	fclose(stdin);
	fclose(stdout);
	return 0;
}
